package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.*;
import com.guochenglang.www.service.ArticleService;
import com.guochenglang.www.service.BaseService;
import com.guochenglang.www.service.CollectService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 关注收藏Controller层
 *
 * @author GuoChenglang
 */

public class CollectController {
    private static final CollectService COLLECT_SERVICE = MyDependency.returnObject(CollectService.class);
    private static final BaseService BASE_SERVICE = MyDependency.returnObject(BaseService.class);
    private static final ArticleService ARTICLE_SERVICE = MyDependency.returnObject(ArticleService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);

    public static Result collectBase(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //获得要收藏的知识库id
        Integer baseId = GetObject.getValue(request, Integer.class, "baseId");
        //添加进收藏表
        int num = COLLECT_SERVICE.collectBase(baseId, userId);
        Result result = Result.result(num);
        //根据结果是否收藏成功
        if (num >= 0) {
            return result;
        } else {
            return Result.noResult();
        }


    }

    /**
     * 获得收藏知识库表
     */
    public static Result getCollectBases(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得收藏表
        ArrayList<CollectBase> collectBases = COLLECT_SERVICE.collectBases(userId);
        //根据收藏表的知识库id获取知识库信息返回前端显示
        ArrayList<Base> bases = new ArrayList<>();
        for (CollectBase collectBase : collectBases) {
            bases.add(BASE_SERVICE.getBase(collectBase.getBaseId()));
        }
        return Result.result(bases);

    }

    /**
     * 取消收藏
     */
    public static Result cancelCollectBase(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获取知识库id
        Integer baseId = GetObject.getValue(request, Integer.class, "baseId");
        //获取用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //执行取消收藏方法
        int num = COLLECT_SERVICE.deleteCollectBase(baseId, userId);
        return Result.result(num);
    }

    /**
     * 收藏文章
     */
    public static Result collectArticle(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //获得文章id
        Integer articleId = GetObject.getValue(request, Integer.class, "articleId");
        //添加进收藏表中
        int num = COLLECT_SERVICE.collectArticle(articleId, userId);
        //根据num返回执行结果
        if (num >= 0) {
            return Result.result(num);
        } else {
            return Result.noResult();
        }
    }

    /**
     * 获得收藏的文章列表
     */
    public static Result getCollectArticles(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));

        //获得收藏文章列表
        ArrayList<CollectArticle> collectArticles = COLLECT_SERVICE.collectArticles(userId);
        //根据文章id获得文章信息，返回前端显示
        ArrayList<Article> articles = new ArrayList<>();
        for (CollectArticle collectArticle : collectArticles) {
            articles.add(ARTICLE_SERVICE.getArticle(collectArticle.getArticleId()));
        }
        return Result.result(articles);
    }

    /**
     * 取消收藏
     */
    public static Result cancelCollectArticle(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得文章id
        Integer articleId = GetObject.getValue(request, Integer.class, "articleId");
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        int num = COLLECT_SERVICE.deleteCollectArticle(articleId, userId);
        return Result.result(num);
    }

    /**
     * 关注用户
     */
    public static Result collectUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //获得被关注的用户id
        Integer collectId = GetObject.getValue(request, Integer.class, "collectId");
        //添加进关注表
        //根据num返回执行结果
        int num = COLLECT_SERVICE.collectUser(userId, collectId);
        if (num >= 0) {
            return Result.result(num);
        } else {
            return Result.noResult();
        }
    }

    /**
     * 获得关注表
     */
    public static Result getCollectUsers(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //根据id获得关注的用户信息，一同返回前端页面
        ArrayList<CollectUser> collectUsers = COLLECT_SERVICE.collectUsers(userId);
        ArrayList<User> users = new ArrayList<>();
        for (CollectUser collectUser : collectUsers) {
            users.add(USER_SERVICE.getUser(collectUser.getTheyId()));
        }
        return Result.result(users);
    }

    /**
     * 取消关注
     */
    public static Result cancelCollectUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得关注的用户id
        Integer collectId = GetObject.getValue(request, Integer.class, "collectId");
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        int num = COLLECT_SERVICE.deleteCollectUser(collectId, userId);
        return Result.result(num);

    }

    /**
     * 获得关注用户的文章信息
     */
    public static Result collectUserArticle(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得关注用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得文章信息并返回
        ArrayList<Article> articles = COLLECT_SERVICE.collectUserArticles(userId);
        return Result.result(articles);
    }

    /**
     * 获取共同关注
     */
    public static Result ourCollectUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得另一个用户id
        Integer id = Integer.valueOf(request.getParameter("id"));
        ArrayList<User> users = new ArrayList<>();
        //获取共同关注
        ArrayList<CollectUser> collectUsers = COLLECT_SERVICE.ourCollectUser(userId, id);
        //如果为空返回空结果
        if (collectUsers.isEmpty()) {
            return Result.result(users);
        }
        //不为空就获取关注的用户列表
        for (CollectUser collectUser : collectUsers) {
            User user = USER_SERVICE.getUser(collectUser.getTheyId());
            users.add(user);
        }
        return Result.result(users);
    }
}
